/**
 * 
 */
package org.gaming.prefab.thing.bign;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.gaming.prefab.thing.IThingType;
import org.gaming.prefab.thing.ThingStorer;
import org.gaming.prefab.thing.cost.CostReceipt;
import org.gaming.prefab.thing.cost.ICostThing;
import org.gaming.prefab.thing.reward.IRewardThing;
import org.gaming.prefab.thing.reward.RewardReceipt;

/**
 * @author YY
 *
 */
public abstract class BignThingStorer<RReceipt extends RewardReceipt<BigDecimal>, CReceipt extends CostReceipt<BigDecimal>>
		extends ThingStorer<BigDecimal, IRewardThing<BigDecimal>, ICostThing<BigDecimal>, RReceipt, CReceipt> {

	private static final Map<IThingType, BignThingStorer<?, ?>> STORERS = new HashMap<>();

	public BignThingStorer() {
		STORERS.put(thingType(), this);
	}

	public static BignThingStorer<?, ?> getStorer(IThingType thingType) {
		return STORERS.get(thingType);
	}

	@Override
	protected boolean isEnough(long entityId, ICostThing<BigDecimal> thing) {
		requireCostPositive(thing);
		BigDecimal currCount = getCount(entityId, thing.getThingId());
		BigDecimal remainCount = currCount.subtract(thing.getNumber());
		boolean enough = remainCount.compareTo(BigDecimal.ZERO) >= 0;
		if(!enough) {
			logger.info("物品{}期望消耗{}，实际只有{}", thing.getThingId(), thing.getNumber(), currCount);
		}
		return remainCount.compareTo(BigDecimal.ZERO) >= 0;
	}

	@Override
	protected void requireCostPositive(ICostThing<BigDecimal> cost) {
		if (cost.getNumber().compareTo(BigDecimal.ZERO) <= 0) {
			throw new RuntimeException("物品数量必须大于0，实际：" + cost.getNumber());
		}
	}

	@Override
	protected void requireRewardPositive(IRewardThing<BigDecimal> reward) {
		if (reward.getNumber().compareTo(BigDecimal.ZERO) <= 0) {
			throw new RuntimeException("物品数量必须大于0，实际：" + reward.getNumber());
		}
	}

}
